<?php 

class AskControl extends CommonControl{
	public function index(){
		//分配top数据
		$this->assign_data();
		// 获取顶级分类
		$data=M('category')->where('pid=0')->all();
		//获取金币
		if(isset($_SESSION['uid'])){
			$uid=(int)$_SESSION['uid'];
		$point=M('user')->field('point')->where('uid='.$uid)->find();
		$point=$point['point'];
		$this->assign('point',$point);

	}
		
		
		$this->assign('data',$data);
		
		$this->display();
	}
	/**
	 * [get_cate ajax分类显示]
	 * @return [type] [description]
	 */
	public function get_cate(){
		if (IS_AJAX) {
			if(!empty($_GET['pid'])){
				$pid=$_GET['pid'];
				$cate=M('category')->where('pid='.$pid)->all();
				if($cate!=null){
				echo json_encode($cate);
			} else {
				echo 0;
			}
			}
		}
	}
	/**
	 * 用户提问
	 */
	public function sub_ask(){
		if(!IS_POST) $this->error('页面不存在');

		$uid =(int)$_SESSION['uid'];
		$reward =(int)$_POST['reward'];


		$data = array(
			'content' => $_POST['content'],
			'time'	  => time(),
			'reward'  => $reward,
			'uid'	  => $uid,
			'cid'	  => $_POST['cid']
			);
		
		
		// var_dump($reward);
		$usid=M('ask')->add($data);
		// 减少金币增加经验和增加提问数
			$db=M('user');
			$ask=(int)C('LV_ASK');
			$b1=$db->exec("UPDATE hd_user set point=point-{$reward} where uid={$uid}");
			$b2=$db->exec("UPDATE hd_user set ask=ask+1 where uid={$uid}");
			$b3=$db->exec("UPDATE hd_user set exp=exp+{$ask} where uid={$uid}");





		// $db = M('user');

		// $db->dec('point', "uid=$uid", $reward);
		// $db->inc('ask', "uid=$uid", 1);
		// $db->inc('exp', "uid=$uid", C('LV_ASK'));

		if($usid&&$b2&&$b3)$this->success('提问成功',__ROOT__.'?c=show&asid='.$usid);

	}
}
 ?>